﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Security;
using MvcApplication2.Models;



namespace MvcApplication2.Controllers
{
    public class AccountController : Controller
    {
        private BaseDBContext db = new BaseDBContext();

        //
        // GET: /Account/LogOn

        public ActionResult LogOn()
        {
            return View();
        }

        //
        // POST: /Account/LogOn

        [HttpPost]
        public ActionResult LogOn(LogOnModel model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                IQueryable<User> a = db.Users.Where(s => (s.Login.Equals(model.UserName) && s.Password.Equals(model.Password)));
                if (a.Count() > 0)
                {
                    User b = a.First();
                    Session["LoggedId"] = b.UserID;
                    Session["LoggedLogin"] = b.Login;
                    Session["LoggedAdmin"] = b.Admin;

                    if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
                        && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
                    {
                        return Redirect(returnUrl);
                    }
                    else
                    {
                        return RedirectToAction("Index", "Home");
                    }
                }
                else{
                    ModelState.AddModelError("", "Podano nieprawidłowy login i/lub hasło.");
                }

            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }

        //
        // GET: /Account/LogOff

        public ActionResult LogOff()
        {
            Session["LoggedId"] = null;
            Session["LoggedLogin"] = null;
            Session["LoggedAdmin"] = null;

            return RedirectToAction("Index", "Home");
        }

        //
        // GET: /Account/Register

        public ActionResult Register()
        {
            return View();
        }

        //
        // POST: /Account/Register

        [HttpPost]
        [CaptchaValidator]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Register(Register user, bool captchaValid) 
        {
            if (!captchaValid)
            {
                ModelState.AddModelError("recaptcha", "Wyrazy zostały niepoprawnie przepisane.");
                return View();
            }

            if (ModelState.IsValid)
            {
                IQueryable<User> a = db.Users.Where(s => (s.Login.Equals(user.Login)));
                if (a.Count() == 0)
                {
                    user.Admin = false;
                    user.Blocked = false;
                    db.Users.Add(user);
                    db.SaveChanges();
                    Session["LoggedId"] = user.UserID;
                    Session["LoggedLogin"] = user.Login;
                    Session["LoggedAdmin"] = user.Admin;
                    return RedirectToAction("Index", "Home");
                }
                else
                {
                    ModelState.AddModelError("", "Podany login jest już zajęty");
                }
            }

            // If we got this far, something failed, redisplay form
            return View(user);
        }

    }
}
